package cn.piesat.controller;

import cn.piesat.util.ResponseWrapperUtil;
import cn.piesat.vo.resource.*;
import cn.piesat.service.intf.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 资源信息访问接口
 *
 * @author xhl
 */
@RestController
@RequestMapping("resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 添加资源（按钮、接口）信息
     * @param dto 添加资源信息参数对象
     * @return
     */
    @PostMapping("add")
    public String add(@Valid @RequestBody AddResourceVO dto) {
        Map<String,String> resultMap = null;
        try {
            resultMap = resourceService.add(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseWrapperUtil.executeCustomTips("false","添加"+dto.getType()+",接口异常");
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * 修改资源（按钮、接口信息）
     * @param dto 修改资源信息参数对象
     * @return
     */
    @PostMapping("update")
    public String update(@Valid @RequestBody UpdateResourceVO dto) {
        Map<String,String> resultMap = null;
        try {
            resultMap = resourceService.update(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseWrapperUtil.executeCustomTips("false","修改"+dto.getType()+",接口异常");
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * 分页搜索资源信息列表
     * @param pageable 分页信息对象
     * @param searchDTO 搜索参数对象
     * @return
     */
    @PostMapping("list")
    public String list(@PageableDefault(sort = "createTime",direction = Sort.Direction.DESC)Pageable pageable,
                       @Valid @RequestBody ResourceSearchVO searchDTO) {
        Map<String,Object> dataMap = resourceService.list(pageable,searchDTO);
        return ResponseWrapperUtil.executeResultMap(dataMap);
    }

    @PostMapping("listAll")
    public String listAll(@Valid @RequestBody ResourceSearchVO searchDTO) {
        List<ResourceVO> list = resourceService.listAll(searchDTO);
        return ResponseWrapperUtil.executeResultList(list);
    }

    /**
     * 通过ID获取详细信息
     * @param id 资源ID
     * @return
     */
    @GetMapping("get/{id}")
    public String getOne(@PathVariable("id") String id) {
        ResourceDetailVO resultVO = resourceService.getOne(id);
        return ResponseWrapperUtil.executeResultEntity(resultVO);
    }

    @GetMapping("delete/{id}")
    public String deleteOne(@PathVariable("id") String id) {
        try {
            resourceService.deleteOne(id);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","删除资源接口异常" + e.getMessage());
        }
        return ResponseWrapperUtil.executeResultTips(true);
    }

    @PostMapping("lock")
    public String lock(@RequestBody LockResourceVO lockResourceVO) {
        try {
            resourceService.lock(lockResourceVO);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","锁定资源接口异常" + e.getMessage());
        }
        return ResponseWrapperUtil.executeResultTips(true);
    }

    @PostMapping("logStatus")
    public String logStatus(@RequestBody LockResourceVO lockResourceVO) {
        try {
            resourceService.logStatus(lockResourceVO);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","是否记录日志资源接口异常" + e.getMessage());
        }
        return ResponseWrapperUtil.executeResultTips(true);
    }
}
